install.packages("modelsummary")
getwd()

install.packages("pandoc")

install.packages("dplyr")
install.packages("dwplot")
install.packages("magrittr")
install.packages("dotwhisker")
packageVersion("dotwhisker")
install.packages("arsenal")  
install.packages("readxl")
install.packages("stargazer")  

library(modelsummary)
library(pandoc)

library(broom)  # 用于整理模型结果
library(ggplot2)
library(dotwhisker)
library("readxl")
library(writexl)
library(texreg)
library(foreign)
library(magrittr) # needs to be run every time you start R and want to use %>%
library(dplyr)    # alternatively, this also loads %>%
library(dwplot)    # alternatively, this also loads %>%ff
library(stargazer)
library(sjPlot)
library(readr)
library(tableby)
library(readxl)         
library(arsenal)         

###### Import Data###### 
Air_RRDV <- read_xlsx('Air_RRDV.xlsx')
WaterResource_RRDV <- read_xlsx('WaterResource_RRDV.xlsx')
Green_RRDV <- read_xlsx('Green_RRDV.xlsx')
WaterSaving_RRDV <- read_xlsx('WaterSaving_RRDV.xlsx')
Noise_RRDV <- read_xlsx('Noise_RRDV.xlsx')
Wetland_RRDV <- read_xlsx('Wetland_RRDV.xlsx')
WaterPollution_RRDV <- read_xlsx('WaterPollution_RRDV.xlsx')


CombinedData_RRDV <- rbind( WaterResource_RRDV, Air_RRDV, WaterSaving_RRDV ,  Green_RRDV,  Noise_RRDV,  Wetland_RRDV, WaterPollution_RRDV)
CombinedData_RRDV$Policy.f <- factor(CombinedData_RRDV$Policy)

## Supplementary model for A9 for new dependent variable measuring the average simialr score

fitRRDV_1 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + absPopulation_learner_leader + absBirthRate_learner_leader + abs第二产业_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = WaterResource_RRDV , na.action = na.omit)
fitRRDV_2 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + absPopulation_learner_leader + absBirthRate_learner_leader + abs第二产业_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Air_RRDV , na.action = na.omit)
fitRRDV_3 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + absPopulation_learner_leader + absBirthRate_learner_leader + abs第二产业_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Green_RRDV , na.action = na.omit)
fitRRDV_4 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + absPopulation_learner_leader + absBirthRate_learner_leader + abs第二产业_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = WaterSaving_RRDV , na.action = na.omit)
fitRRDV_5 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + absPopulation_learner_leader + absBirthRate_learner_leader + abs第二产业_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f , data = Noise_RRDV , na.action = na.omit)
fitRRDV_6 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + absPopulation_learner_leader + absBirthRate_learner_leader + abs第二产业_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = Wetland_RRDV , na.action = na.omit)
fitRRDV_7 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + absPopulation_learner_leader + absBirthRate_learner_leader + abs第二产业_learner_leader+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = WaterPollution_RRDV , na.action = na.omit)
fitRRDV_8 <- lm(similarity ~ horizon_learner +  Distance + differ_year +  innovator_learner_leader + SimilarSpecialization + city_learner + Self_learer.f + ABSforeigInvestmentBillion_learner_leader + absPopulation_learner_leader + absBirthRate_learner_leader + abs第二产业_learner_leader + Policy.f+ Learner_ID.f + Leader_ID.f + Year_learner.f, data = CombinedData_RRDV , na.action = na.omit)


models <- list(
  "Overall" = fitRRDV_8,
  "Air Pollution" = fitRRDV_2,
  "Green Space" = fitRRDV_3,
  "Noise Pollution" = fitRRDV_5,
  "Water Resource" = fitRRDV_1,
  "Water Saving" = fitRRDV_4,
  "Wetland" = fitRRDV_6,
  "Water Pollution" = fitRRDV_7
)

modelsummary(models,
             coef_map = c(
               "horizon_learner" = "Horizontal Learning",
               "Distance" = "Spatial Distance",
               "differ_year" = "Temporal Distance",
               "innovator_learner_leader" = "Initial Learning",
               "SimilarSpecialization" = "Similar Judiciary System",
               "city_learner" = "City Learner",
               "Self_learer.f" = "Self-Learning",
               "ABSforeigInvestmentBillion_learner_leader" = "Foreign Investment Difference",
               "absPopulation_learner_leader" = "Population Difference",
               "absBirthRate_learner_leader" = "Birthrate Difference",
               "abs第二产业_learner_leader" = "Secondary Industry Difference",
               "(Intercept)" = "Constant"
             ),
             gof_omit = 'AIC|BIC|Log.Lik', # omitted  
             stars = c('*' = .1, '**' = .05, '***' = .01),  # custom star cutoffs
             notes = "*p<0.1; **p<0.05; ***p<0.01",         # footnote explaining stars
             add_rows = data.frame(
               term = c("Learner Location Dummies", "Learner Year Dummies", "Source Location Dummies", "Policy Fixed Effects"),
               Overall = c("Yes", "Yes", "Yes", "Yes"),
               `Air Pollution` = c("Yes", "Yes", "Yes", "N/A"),
               `Green Space` = c("Yes", "Yes", "Yes", "N/A"),
               `Noise Pollution` = c("Yes", "Yes", "Yes", "N/A"),
               `Water Resource` = c("Yes", "Yes", "Yes", "N/A"),
               `Water Saving` = c("Yes", "Yes", "Yes", "N/A"),
               Wetland = c("Yes", "Yes", "Yes", "N/A"),
               `Water Pollution` = c("Yes", "Yes", "Yes", "N/A")
             ),
             output = "new DV-Table A5.docx"
)
